//
//  MineWalletViewController.m
//  YongChuanTradingArea
//
//  Created by Vivian on 2017/9/11.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "MineWalletViewController.h"
#import "MineWalletHeaderView.h"
#import "MineWalletCell.h"
#import "WithdrawWayViewController.h"

static NSString * const MineWalletCellID = @"MineWalletCell";

@interface MineWalletViewController ()

@property (nonatomic, strong) MineWalletHeaderView *headerView;

@end

@implementation MineWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的钱包";
    [self createUI];
//    [self customBarButtonItemWithTitle:@"提现记录" action:@selector(withdrawListAction) isLeft:NO color:kMainColor];
    [self setupPageHeader:YES footer:YES];
    [self headerRefresh];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


- (void)createUI{
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 60;
    self.headerView = [[NSBundle mainBundle]loadNibNamed:@"MineWalletHeaderView" owner:nil options:nil].lastObject;
    [self.headerView.withdrawBtn addTarget:self action:@selector(withdrawBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = self.headerView;
    [self.tableView registerNib:[UINib nibWithNibName:MineWalletCellID bundle:nil] forCellReuseIdentifier:MineWalletCellID];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 12, 0, 0);
    self.tableView.separatorColor = [UIColor colorWithHexString:@"#dcdcdc"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineWalletCell *cell = [tableView dequeueReusableCellWithIdentifier:MineWalletCellID forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - btn clicked
- (void)withdrawBtnAction{
    if ([LBUserManage defaultManager].userModel.WXUUID.length > 1) {
        WithdrawWayViewController * vc = [[WithdrawWayViewController alloc]init];
        vc.amt = self.headerView.balanceLabel.text;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        __weak typeof(self) weakSelf = self;
        [UIAlertController showWithMessage:@"提现需绑定微信后再试！" controller:self sureAction:^{
            [ShareSDK getUserInfo:SSDKPlatformTypeWechat onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
                NSLog(@"%@ -- %lu", error.description, (unsigned long)state);
                if (state == SSDKResponseStateSuccess) {
                    [LBNetworkingInstance requestGET:bindWechat parameters:@{@"memid":[LBUserManage defaultManager].user_id?:@"",@"uuid":user.uid?:@""} showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
                        if (code == YCNetworkingStatusCodeSuccess) {
                            //                            [LBUserManage defaultManager].userModel = [UserModel yy_modelWithJSON:responseObject[@"Data"]];
                            [LBUserManage defaultManager].userModel.WXUUID = user.uid;
                            WithdrawWayViewController * vc = [[WithdrawWayViewController alloc]init];
                            vc.amt = weakSelf.headerView.balanceLabel.text;
                            [weakSelf.navigationController pushViewController:vc animated:YES];
                        }
                    }];
                } else {
                    [MBProgressHUD showError:@"第三方登录失败，请重试"];
                }
            }];
        }];
    }
    
}

- (void)withdrawListAction{
    
}

#pragma mark - request data
- (void)requestPageData:(void (^)(YCNetworkingStatusCode, NSArray *))statusBlock {
    
    __weak typeof(self) weakSelf = self;
    [LBNetworkingInstance requestGET:userWallet parameters:@{@"memberId":[LBUserManage defaultManager].userModel.ID?:@"", @"pageindex":@(self.pageIndex),@"pagesize":@(self.pageSize)} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        if (code == YCNetworkingStatusCodeSuccess){
            MineWalletModel * model = [MineWalletModel yy_modelWithJSON:responseObject[@"Data"]];
            statusBlock(code, model.UserRewardList);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.headerView.model = model;
                [weakSelf.tableView reloadData];
            });
        }else{
            statusBlock(code, nil);
        }
    } failure:^(NSError *err) {
        statusBlock(YCNetworkingStatusCodeHTTPError, nil);
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
